import { CoreConn } from "@/connect/CoreConn";
import { useActivity } from "@/stores/activity";
import { useKline } from "@/stores/kline";
import { ref, watch } from "vue";

export const useConn = function () {
    const kline = useKline();
    const activity = useActivity();
    const changed = ref(false);
    let tid = 0;

    watch(
        () => kline.map.length,
        n => {
            console.log("##tom 初始化币对列表", n);
            subscribe();
        }
    );

    watch(
        () => kline.curCoin,
        n => {
            console.log("##tom 切换币对", n);
            CoreConn.ins.curSymbolName = n.symbol;
            subscribe();
        }
    );

    watch(
        () => kline.curInterval,
        n => {
            console.log("##tom 切换时间间隔", n);
            CoreConn.ins.curIntervalName = n.name;
            subscribe();
        }
    );

    function subscribe() {
        window.clearTimeout(tid);
        tid = window.setTimeout(() => {
            if (kline.curCoin) {
                CoreConn.ins.start();
            }
        }, 90);
    }

    return {
        subscribe
    };
};
